package com.sunlands.com.commands;

import com.netflix.hystrix.*;
import com.sunlands.com.service.WechatRpcService;

/**
 * Created by yusong on 2018/5/18.
 * 企业微信发消息命令
 */
public class WechatCommand  extends HystrixCommand<String> {

    private String email;
    private String content;

    /**
     *各种参数
     */
    private static final Setter cachedSetter =Setter
            .withGroupKey(HystrixCommandGroupKey.Factory.asKey("SystemX"))
            .andCommandKey(HystrixCommandKey.Factory.asKey("PrimaryCommand"))
            .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("PrimaryCommand"))
            .andCommandPropertiesDefaults(
                    // 超时时间生效
                    HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(30000));

    /**
     * 传入参数
     */
    public WechatCommand( String email,String content) {
        super(cachedSetter);
        this.email = email;
        this.content = content;
    }

    /**
    *你的业务逻辑
    */
    @Override
    protected String run() throws Exception {
        Thread.sleep(3000);
        String res = WechatRpcService.sendMsg(email,content);
        return res;
    }

    /**
     *出错默认返回
     */
    @Override
    protected String getFallback() {
        return "ERROR";
    }
}